<template>
  <div class='sub-category'>
    <!-- v-if="subCategoryData" 控制模板渲染时机 -->
    <!-- 使用v-if控制模板的渲染时机 只有subCategoryData有数据的时候才会正式渲染模板 正式读取数据 -->
    <div class="container" v-if="subCategoryData">
      <!-- 二级类目页面 面包屑导航 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>

        <XtxBreadItem id="active" :to="`/category/${subCategoryData.parentId}`">
          {{ subCategoryData.parentName }}
        </XtxBreadItem>

        <XtxBreadItem>{{ subCategoryData.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 筛选区 -->
      <!-- <SubFilter/> -->
      <div class="sub-filter">
        <!-- 品牌 筛选区 -->
        <div class="item">
          <div class="head">品牌：</div>
          <div class="body">
            <a @click="changeBrand(item)" href="javascript:;" v-for="item in subCategoryData.brands" :key="item.id">
                {{ item.name }}
            </a>
          </div>
          </div>

        <!-- saleProperties 销售属性 筛选区 -->
        <div class="item" v-for="item in subCategoryData.saleProperties" :key="item.id">
          <div class="head">{{ item.name }}</div>
          <div class="body">
            <!-- 绑定事件 + 动态类名判断 -->
            <a href="javascript:;"
            @click="toogleSelected(i, item)"
            :class="{active: i.selected===true}"
            v-for="i in item.properties" :key="i.id">
                {{ i.name }}
            </a>
          </div>
        </div>
      </div>

      <!-- 商品列表区 -->
      <div class="list-container">
        <!-- 排序区域 -->
        <div class='sub-sort'>
          <div class="sort">
            <a @click="changeSortType('publishTime') " :class="{active: sortType==='publishTime'}" href="javascript:;">默认排序</a>
            <a @click="changeSortType('orderNum')" :class="{active: sortType==='orderNum'}" href="javascript:;">最新商品</a>
            <a @click="changeSortType('price')" :class="{active: sortType==='price'}" href="javascript:;">最高人气</a>
            <a @click="changeSortType('evaluateNum')" :class="{active: sortType==='evaluateNum'}" href="javascript:;">评论最多</a>
          </div>
        </div>
        <!-- 商品列表 -->
        <div class="sort-list-container">
          <GoodsItem :good="item"  v-for="item in goodList" :key="item.id"/>
        </div>
      </div>

    </div>

    <!-- 加载更多组件
      1. index.vue 和 加载更多组件 是父子关系
      2. 加载下一页依赖的参数和接口在父组件中 而检测到要加载下一页的动作在子组件中
        需要子组件 在检测到要加载下一页数据了 通知父组件以page++ 去拉取最新数据
      3. 是否数据在加载中  这个条件在哪获取到？
        由于这个状态是在父组件中得到的 子组件需要用这个状态去判断是否继续触发load事件，父传子
      4. 数据是否全部加载完毕  这个条件也是在父组件拿到的  items.length === 0 父传子
    -->
    <XtxInfiniteLoad @load="loadNext" :isLoading="isLoading" :isFinished="isFinished"/>
  </div>
</template>

<script>
import { useCategoryData } from './service/useCategoryData'
import { useGoodList } from './service/useGoodList'
import GoodsItem from '../components/good-item'
export default {
  name: 'SubCategory',
  components: {
    GoodsItem
  },
  setup () {
    // 获取二级类目筛选数据
    const { subCategoryData } = useCategoryData()

    // 二级类目商品数据
    const { goodList, toogleSelected, sortType, reqParams, changeSortType, changeBrand, loadNext, isFinished, isLoading } = useGoodList()

    return { subCategoryData, toogleSelected, sortType, goodList, changeSortType, changeBrand, reqParams, loadNext, isLoading, isFinished }
  }
}
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
}

.xtx-bread-item{
  .active {
    color: @xtxColor
  }
}

 // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      padding: 10px 0;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }

  .list-container {
    margin-top: 20px;
    padding: 0 20px;
    background: #fff;
  }

  // 排序
  .sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all .3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
            &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}

 // 商品列表
 .sort-list-container {
      display: flex;
      flex-wrap: wrap;
      .goods-item {
        flex: 1;
      }
   }
</style>
